package com.yenlien.traceyun.control.controller;

import com.yenlien.traceyun.control.dao.ModelCategoryDao;
import com.yenlien.traceyun.control.entity.ModelCategory;
import com.yenlien.traceyun.control.service.ModelCategoryService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: bigwangy@sina.com
 * @create: 2020-11-18
 * @description:
 **/
@RestController
@RequestMapping(value = "/modecategory")
public class ModelCategoryController extends BaseController {

    @Autowired
    private ModelCategoryService modelCategoryService;

    @Autowired
    private ModelCategoryDao modelCategoryDao;

    /**
     * 获取树撞结构     在企业那边根据企业ID查询基源
     *
     * @param request
     * @return
     */
    @GetMapping(value = "/tree")
    public Object getTree(HttpServletRequest request) {
        return success(modelCategoryService.getTree(request.getParameter("companyId")));
    }

    /**
     * 获取
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/load/{id}")
    public Object load(@PathVariable("id") String id) {
        return modelCategoryService.load(id);
    }

    /**
     * 获取
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/child/{id}")
    public Object child(@PathVariable("id") String id) {
        return modelCategoryService.loadChild(id);
    }

    /**
     * 获取根节点  获取所有一级分类
     *
     * @return
     */
    @GetMapping(value = "/root")
    public Object getRoot() {
        return success(modelCategoryService.getRoot());
    }

    /**
     * 根据一级分类加载所有二级
     *
     * @return
     */
    @GetMapping(value = "/get2Level")
    public Object get2Level(String id) {
        return success(modelCategoryService.get2Level(id));
    }

    /**
     * 添加,修改
     *
     * @return
     */
    @PostMapping(value = "/update")
    public Object add(ModelCategory category, HttpServletRequest request) throws Exception {

        List<MultipartFile> files = null;
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        modelCategoryService.save(category, files);
        return success(category);
    }

    /**
     * 删除
     *
     * @return
     */
    @GetMapping(value = "/dele/{id}/{type}")
    public Object delete(@PathVariable("id") String id, @PathVariable("type") int type) {
        modelCategoryService.dele(id, type);
        return success("ok");
    }

    /**
     * 在中控根据应用版本ID请求基源的接口 /tree的结果要和该接口的获取的基源种类一样才正确，以该接口为准
     *
     * @param customAppVersionId
     * @return
     */
    @GetMapping(value = "/loadroot/cusomappversion/{customAppVersionId}")
    public Object loadRootCustomAppVersion(@PathVariable("customAppVersionId") String customAppVersionId) {
        return modelCategoryDao.findRootByCustomAppVersionId(customAppVersionId);
    }

    /**
     * 政管和企业根据ID获取对应基源一级种类， 使用该接口替换企业和政管的 tree 接口
     *
     * @param companyId
     * @return
     */
    @GetMapping(value = "/loadroot/company/{companyId}")
    public Object loadRootByCompanyId(@PathVariable("companyId") String companyId) {
        return modelCategoryService.loadRootByCompanyId(companyId);
    }

}
